var vm = new Vue({
    el: "#app",
    data: {
        url:'http://127.0.0.1:7002',//netty上传接口
        src:'',
        dialogVisible:false,
        dialogVisibleByLb:false,
        selectedLbCode:'',
        fileList:[],
        file:{},
        fileTmp:[],//用于展现文件
        selectedRow:{},
        dialogName:'',
        dialogNameByLb:'',
        openDialogType:'',
        openDialogTypeByLb:'',
        currentTreeNode:{},
        ruleForm:{
            type:'',
            fileName:'',
            optType:'',
        },
        fileLbForm:{
            lbCode:'',
            lbName:'',
            pid:'',
        },
        fileTypes:[{
            value: '0',
            label: 'war'
        }, {
            value:'1',
            label:'GIT'
        }],
        rules: {
            fileName: [
                { required: true, message: '请输入文件名' },
            ],
            type: [
                { required: true, message: '请选择类型'},
            ]
        },
        lbCodeInfo: [],
        lbTreeProps: {
            children: 'fileLBDos',
            label: 'lbName'
        }
    },
    mounted:function(){

    },
    created:function(){
        var that = this;
        /**
         * 加载树
         */
        that.queryLb();
        /**
         * 加载列表
         */

    },
    methods: {
        append:function(data,treeList){
            var that = this;
            that.fileLbForm={}
            that.dialogVisibleByLb=true;
            that.openDialogTypeByLb=1;
            that.fileLbForm.pid=data.data.lbId;
            that.showDialog("新增类别")

        },
        update:function(data,treeList){
            var that = this;
            that.dialogVisibleByLb=true;
            that.openDialogTypeByLb=2;
            $.extend(true,that.fileLbForm,data.data);
            that.showDialog("修改类别")
        },
        remove:function(data,treeList){
            var that = this;
            $.ajax({
                type: "post",
                url: "/fileController/delFileLb",
                dataType: "json",
                contentType : 'application/x-www-form-urlencoded',
                data:{"lbCode":data.data.lbCode},
                success: function(json){
                    if(json.status==200){
                        that.$message(json.msg);
                        that.dialogVisibleByLb=false;
                        that.openDialogTypeByLb='';
                        that.queryLb();
                    }else {
                        that.$message(json.msg);
                    }
                }
            });
        },
        addLb:function(){
            var that = this;
            that.dialogVisibleByLb=true;
            that.openDialogTypeByLb=1;
            that.showDialog("新增类别（root）")
        },
        queryLb:function (){
            var that = this;
            $.getJSON("/fileController/queryFileLb", function(json){
                if(json.status==200){
                    that.lbCodeInfo=json.data;
                    that.$nextTick(() => {
                        if(that.lbCodeInfo==null || that.lbCodeInfo.length==0){
                            return
                        }
                        var firstLeaf = that.getFirstElement(that.lbCodeInfo);
                        console.log(firstLeaf)
                        that.$refs.tree.setCurrentNode(firstLeaf);
                        that.currentTreeNode = firstLeaf;
                        that.query();
                    });
                }else {
                    that.$message(json.msg);
                }
            });

        },
        getFirstElement:function (arr){
            var item = arr[0]
            if(item.fileLBDos!=null && item.fileLBDos.length>0){
                return this.getFirstElement(item.fileLBDos);
            }else{
                return item;
            }
        },
        submitFormByLb:function (fileLbForm){
            var that = this;
            var url = ""
            if(that.openDialogTypeByLb==1){
                url = "/fileController/addFileLb"
            }
            if(that.openDialogTypeByLb==2){
                url = "/fileController/updateFileLb"
            }
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                contentType:"application/json",
                data:JSON.stringify(that.fileLbForm),
                success: function(json){
                    if(json.status==200){
                        that.$message(json.msg);
                        that.dialogVisibleByLb=false;
                        that.openDialogTypeByLb='';
                        that.queryLb();
                    }else {
                        that.$message(json.msg);
                    }
                }
            });
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleerror(err, file, fileList){
            console.log(err)
            console.log(file)
            console.log(fileList)
        },
        handleNodeClick:function (data) {
            this.currentTreeNode=data
            this.query();
        },
        add:function(){
            /**
             * 新增文件
             * @type {string}
             */
            this.ruleForm={};
            var node = this.$refs.tree.getCurrentNode();
            if(node==null){
                this.$message("请选择文件类别末级节点！");
                if(node.fileLBDos!=null && node.fileLBDos.length>0){
                    this.$message("请选择文件类别末级节点！");
                    return;
                }
                return ;
            }
            this.openDialogType='1';
            this.dialogVisible=true
            this.currentTreeNode=node;
            this.showDialog("新增")
        },
        del:function(index,row){
            var that = this
            $.ajax({
                url: "/fileController/delFile",
                type: 'post',
                contentType : 'application/x-www-form-urlencoded',
                data: {"fileIds":row.fileId},
                success: function (data) {
                    if(data.status==200){
                        that.query();
                    }else {
                        that.$message(data.msg);
                    }
                }
            })
        },
        delBatch:function(){
            var that = this
            var currentSelect = this.$refs.selectedRow.selection;
            if(!currentSelect || currentSelect.length<=0){
                this.$message("请选择待删除记录！");
                return;
            }
            var ids = [];
            currentSelect.forEach(function(e){
                ids.push(e.fileId)
            })
            $.ajax({
                url: "/fileController/delFile",
                type: 'post',
                contentType : 'application/x-www-form-urlencoded',
                data: {"fileIds":ids.join(",")},
                success: function (data) {
                    if(data.status==200){
                        that.query();
                    }else {
                        that.$message(data.msg);
                    }
                }
            })
        },
        fixChange:function(file, fileList){
            this.file = file.raw
            this.ruleForm.fileName=file.name;
        },
        fix:function(index,row){
            this.ruleForm={};
            this.openDialogType='2';
            this.dialogVisible=true
            this.fileTmp = [{"name":row.fileName,"url":row.fileId}]
            this.showDialog("文件更新",row)
        },
        download:function(index,row){
            var a = document.createElement("a");
            a.download =this.url+"/download?fileId="+row.fileId;
            a.href = this.url+"/download?fileId="+row.fileId;
            $("body").append(a); // 修复firefox中无法触发click
            a.click();
            $(a).remove();
        },
        query:function(){
            /**
             * 查询文件列表
             */
            var form = new FormData();
            form.append("lbCode",this.currentTreeNode.lbCode);
            var that = this;
            $.ajax({
                url: "/fileController/queryFileByLbCode",
                type: 'post',
                processData: false,
                contentType: false,
                data: form,
                success: function (data) {
                    if(data.status==200){
                        that.fileList=data.data;
                    }else {
                        that.$message(data.msg);
                    }
                }
            })
        },
        resetForm:function (ruleForm){
            console.log(ruleForm)
        },
        uploadZDY:function (item){
            console.log(item);
            var fd = new FormData();
            fd.append("fileName", this.ruleForm.fileName);
            //更新
            if( this.openDialogType=='2'){
                fd.append("optType","UPDATEFILE");
                fd.append("fileId",this.ruleForm.fileId);
            }
            //新增
            if( this.openDialogType=='1'){
                fd.append("optType","ADDFILE");
            }
            fd.append("type",this.ruleForm.type);
            fd.append("lbCode",this.currentTreeNode.lbCode);
            if(item.file){
                fd.append("file", item.file);
            }
            var that = this;
            $.ajax({
                url: that.url,
                type: 'post',
                processData: false,
                contentType: false,
                data: fd,
                dataType:"text",
                success: function (data) {
                    console.log(data)
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log(XMLHttpRequest)
                    console.log(textStatus)
                    console.log(errorThrown)
                }
            })
        },
        submitForm:function (formName){
            var fd = new FormData();
            fd.append("fileName", this.ruleForm.fileName);
            //更新
            if( this.openDialogType=='2'){
                fd.append("optType","UPDATEFILE");
                fd.append("fileId",this.ruleForm.fileId);
            }
            //新增
            if( this.openDialogType=='1'){
                fd.append("optType","ADDFILE");
            }
            fd.append("type",this.ruleForm.type);
            fd.append("lbCode",this.currentTreeNode.lbCode);
            if(this.file.name){
                fd.append("file",this.file);
            }
            var that = this;
            $.ajax({
                url: that.url,
                type: 'post',
                processData: false,
                contentType: false,
                data: fd,
                async:false,
                dataType:"text",
                success: function (data) {
                    if(data.errorCode=='0'){
                        that.query();
                    }else{
                        that.$message(data.msg);
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log(XMLHttpRequest)
                    console.log(textStatus)
                    console.log(errorThrown)
                }
            })
        },
        showDialog:function (dialogName,row){
            if(this.dialogVisible){
                this.dialogName = dialogName;
                if(row){
                    this.ruleForm=row;
                }

            }
            if(this.dialogVisibleByLb){
                this.dialogNameByLb = dialogName;
                if(row){
                    this.fileLbForm=row;
                }
            }

        }

    }
})